import customerApi from "@/api/sale/customer";

export default {
  name: "customer",
  data() {
    return {
      formVisible: false,
      formTitle: "添加客户",
      listQuery: {
        current: 1,
        size: 20,
        criteria: {
          name: "",
        },
      },
      total: 0,
      list: null,
      listLoading: true,
      selRow: {},
      form: {
        companyName:"",
        name: "",
        id: "",
        address: "",
        phoneNumber: "",
        contact: "",
        email: "",
        remark: "",
        num: "",
        status: "",
      },
      rules:{
        name: [
          { required: true, message: "请输入客户名称", trigger: "blur" },
          {
            min: 2,
            max: 25,
            message: "长度在 2 到 25 个字符",
            trigger: "blur",
          },
        ],
        companyName:[
          { required: true, message: "请输入公司名称", trigger: "blur" },
        ],
          phoneNumber:[
            { required: true, message: "请输入手机号码", trigger: "blur" },
          ],
        
      }
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.fetchData();
    },
    fetchData() {
      this.listLoading = true;
      customerApi.getList(this.listQuery).then(response => {
          this.list = response.data.records
          this.listLoading = false
          this.total = response.data.total
        })
    },
    formatDate(originVal) {
      const dt = new Date(originVal);
      const y = dt.getFullYear();
      const m = (dt.getMonth() + 1 + "").padStart(2, "0");
      const d = (dt.getDate() + "").padStart(2, "0");
      const hh = (dt.getHours() + "").padStart(2, "0");
      const mm = (dt.getMinutes() + "").padStart(2, "0");
      const ss = (dt.getSeconds() + "").padStart(2, "0");
      return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
    },
    search() {
      this.listQuery.current = 1;
      this.fetchData();
    },
    reset() {
      this.listQuery.criteria.name = "";
      this.listQuery.current = 1;
      this.fetchData();
    },
    handleFilter() {
      this.listQuery.current = 1;
      this.fetchData();
    },
    handleClose() {},
    fetchNext() {
      this.listQuery.current = this.listQuery.current + 1;
      this.fetchData();
    },
    fetchPrev() {
      this.listQuery.current = this.listQuery.current - 1;
      this.fetchData();
    },
    fetchPage(page) {
      this.listQuery.current = page;
      this.fetchData();
    },
    changeSize(limit) {
      this.listQuery.size = limit;
      this.fetchData();
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow;
    },
    add() {
      this.resetForm();
      this.formTitle = "添加客户";
      this.form.num =this.total+1
      this.formVisible = true;
    },
    resetForm(){
      this.form= {
        companyName:"",
        name: "",
        id: "",
        address: "",
        phoneNumber: "",
        contact: "",
        email: "",
        remark: "",
        num: "",
        status: "",
      }
    },
    save(){
      let self = this;
            this.$refs["form"].validate((valid) => {
              if (valid) {
                customerApi.save(this.form).then((response) => {
                  this.$message({
                    message: "提交成功",
                    type: "success",
                  });
                  self.fetchData();
                  self.formVisible = false;
                });
              } else {
                return false;
              }
            });
    },
    changeStatus(id){
      
      if(id){
        customerApi.changeStatus(id).then((response) => {
          this.$message({
            message: "提交成功",
            type: "success",
          });
          this.fetchData();
        });
      }
    },
    editItem(row){
      let form = Object.assign({},row);
        this.form = form
        this.formTitle = '修改客户信息'
        this.formVisible = true
    },
    edit() {
      if (this.checkSel()) {
        let form = Object.assign({}, this.selRow);
        this.form = form
        this.formTitle = '修改客户信息'
        this.formVisible = true
      }
    },
    checkSel() {
      if (this.selRow && this.selRow.id) {
        return true
      }
      this.$message({
        message: '请选中操作项',
        type: 'warning'
      })
      return false
    },

    removeItem(record){
          this.selRow = record
          this.remove()
        },
        remove() {
          if (this.checkSel()) {
            let id = this.selRow.id
    
            this.$confirm('确定删除该记录?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              customerApi.remove(id).then((response) => {
                this.$message({
                  message: '删除成功',
                  type: 'success'
                })
                this.fetchData()
              })
            }).catch(() => {
            })
          }
        },
  },
};
